/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: managerLink.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月17日 星期三 05时32分55秒
***************************************************/

#include "managerLink.h"


//申请头节点
VIP_M *createManagerHead(void){
	VIP_M *head = NULL;
	head = (VIP_M*)malloc(sizeof(VIP_M));
	memset(head,0,sizeof(VIP_M));
	return head;
}

//创建链表
VIP_M *createManagerLink(VIP_M *head){
	assert(head);
	VIP_M *cur = NULL;
	cur = head;
	VIP_M *new = NULL;

	int n = 0;
	while(1){
		printf("input new vip id:\n");
		printf("id = 0时，完成会员信息录入\n");
		printf("vip id:");
		scanf("%d",&n);

		if(n == 0){
			break;
		}

		new = (VIP_M*)malloc(sizeof(VIP_M));
		memset(new,0,sizeof(VIP_M));

		//输入vip信息
		if(n >= 99999 && n < 1000000){
			new->data.id = n;
		}else{
			printf("id input error!\n");
			continue;
		}
		printf("name:");
		scanf("%s",new->data.name);
		while(1){
			bzero(new->data.idNumber,sizeof(new->data.idNumber));

			printf("idNumber:");
			scanf("%s",new->data.idNumber);

			if(strlen(new->data.idNumber) == 18){
				break;
			}else{
				printf("idNumber input error!\n");
			}
		}
		while(1){
			bzero(new->data.tel,sizeof(new->data.tel));
			
			printf("tel:");
			scanf("%s",new->data.tel);

			if(strlen(new->data.tel) == 11){
				break;
			}else{
				printf("tel input error!\n");
			}
		}
		printf("balance:");
		scanf("%f",&new->data.balance);
		while(1){
			bzero(&new->data.state,sizeof(&new->data.state));

			printf("state(正常：1 挂失：0):");
			scanf("%d",&new->data.state);

			if(new->data.state == 1 || new->data.state == 0){
				break;
			}else{
				printf("state input error!\n");
			}
		}
		

		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}

	return head;
}

//删除链表
VIP_M *delManagerLink(VIP_M *head,int n){
	assert(head);
	VIP_M * cur = head->next;
	VIP_M * bef = head;

	printf("n=%d\n",n);
	while(cur != NULL){
		if(cur->data.id == n){
			break;
		}
		bef = cur;
		cur = cur->next;
	}
	bef->next = cur->next;
	free(cur);
	cur = NULL;
	return head;	
}

//修改链表
VIP_M *amendManagerLink(VIP_M *head,int n){
	assert(head);
	VIP_M * cur = NULL;
	cur = head;

	int model = 0;
	char str[20] = {0};
	while(cur != NULL){
		if(cur->data.id == n){
			while(1){
				printf("1--修改姓名\n");
				printf("2--修改身份证号\n");
				printf("3--修改联系电话\n");
				printf("0--退出\n");

				printf("请选择需要修改的项目：");
				scanf("%d",&model);

				if(model == 0){
					break;
				}
				switch(model){
					case 1:
						bzero(cur->data.name,20*sizeof(char));
						printf("修改后的姓名：");
						scanf("%s",str);
						strcpy(cur->data.name,str);
						break;
					case 2:
						bzero(cur->data.idNumber,20*sizeof(char));
						printf("修改后的身份证号：");
						scanf("%s",str);
						strcpy(cur->data.idNumber,str);
						break;
					case 3:
						bzero(cur->data.tel,12*sizeof(char));
						printf("修改后的联系电话：");
						scanf("%s",str);
						strcpy(cur->data.tel,str);
						break;
					default:
						printf("输入错误！\n");
						break;
				}
			}

			break;
		}
		cur = cur->next;
	}
	return head;	
}
//链表输出函数
void outputManagerLink(const VIP_M *head){
	assert(head);
	VIP_M *cur = NULL;
	cur = head->next;

	while(cur != NULL){
		printf("id:%d\tname:%s\tidNumber:%s\ttel:%s\tbalance:%.2f\tstate:%d\n",\
				cur->data.id,cur->data.name,cur->data.idNumber,cur->data.tel,\
				cur->data.balance,cur->data.state);
		cur = cur->next;
	}
}

//链表释放函数
void freeManagerLink(VIP_M *head){
	assert(head);
	VIP_M *tmp = NULL;
	VIP_M *cur = NULL;
	cur = head;

	while(cur != NULL){
		tmp = cur->next;
		free(cur);
		cur = NULL;
		cur = tmp;
	}
}

//会员信息写入文件函数
FILE *vipInfoWriteToFile(FILE *fp,const VIP_M *head){
	assert(head);
	VIP_M *cur = NULL;
	cur = head->next;

	while(cur != NULL){
		fwrite(cur,sizeof(VIP_M),1,fp);
		cur = cur->next;
	}
	cur = NULL;
	return fp;
}

//会员信息读出文件函数
VIP_M *vipInfoReadFile(FILE *fp,VIP_M *head){
	assert(head);
	VIP_M *cur = NULL;
	cur = head;
	VIP_M *new = NULL;

	size_t ld = 1;
	while(ld != 0){
		new = (VIP_M*)malloc(sizeof(VIP_M));
		memset(new,0,sizeof(VIP_M));

		ld = fread(new,sizeof(VIP_M),1,fp);

		if(ld == 0){
			free(new);
			new = NULL;
			break;
		}
		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}

	return head;
}
